/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <glib.h>

#include <owe/global.h>
#include <owe/ustring.h>

#include "ustring_p.h"

OWE_USE_NAMESPACE

UStringPrivate::UStringPrivate(const char *str)
    : string(0)
{
    if (str)
        string = g_strdup(str);
    else
        string = g_strdup("");
}

UStringPrivate::~UStringPrivate()
{
    g_free(string);
}

UString::UString(const char *str)
    : d_ptr(new UStringPrivate(str))
{
}

UString::~UString()
{
    delete d_ptr;
}

UString::UString(const UString &other)
    : d_ptr(new UStringPrivate(0))
{
    g_free(d_ptr->string);
    d_ptr->string = g_strdup(other.d_ptr->string);
}

UString &UString::operator=(const UString &other)
{
    if (d_ptr->string == other.d_ptr->string)
        return *this;

    g_free(d_ptr->string);

    d_ptr->string = g_strdup(other.d_ptr->string);

    return *this;
}

int UString::compare(const char *uchar) const
{
    if (!uchar)
        return -1;

    return g_utf8_collate(d_ptr->string, uchar);
}

UString &UString::append(const UString &ustr)
{
    char *temp = d_ptr->string;

    d_ptr->string = g_strconcat(d_ptr->string, ustr.d_ptr->string, NULL);
    g_free(temp);

    return *this;
}

int UString::size() const
{
    return g_utf8_strlen(d_ptr->string, -1);
}

const char *UString::data() const
{
    return d_ptr->string;
}

UString &UString::simplified()
{
    d_ptr->string = g_strstrip(d_ptr->string);

    return *this;
}
